package codediff.parse.report;

import codediff.parse.report.enumeration.CounterType;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Getter
@Setter
@JsonIgnoreProperties
@NoArgsConstructor
@AllArgsConstructor
public class Counter {
    private CounterType type;
    private Integer missed;
    private Integer covered;

    public static Counter newInstruction(List<CoverageLine> newLines) {
        int newLineSumMi = newLines.stream().map(CoverageLine::getMi).mapToInt(it -> it).sum();
        int newLineSumCi = newLines.stream().map(CoverageLine::getCi).mapToInt(it -> it).sum();
        return new Counter(CounterType.NEW_INSTRUCTION, newLineSumMi, newLineSumCi);
    }

    public static Counter newLine(List<CoverageLine> newLines) {
        int newLineSumCL = Math.toIntExact(newLines.stream().map(it -> (it.getCi() + it.getCb()) > 0).count());
        int newLineSumML = newLines.size() - newLineSumCL;
        return new Counter(CounterType.NEW_LINE, newLineSumML, newLineSumCL);
    }

    public static Counter newBranch(List<CoverageLine> newLines) {
        int newLineSumMb = newLines.stream().map(CoverageLine::getMb).mapToInt(it -> it).sum();
        int newLineSumCb = newLines.stream().map(CoverageLine::getCb).mapToInt(it -> it).sum();
        return new Counter(CounterType.NEW_BRANCH, newLineSumMb, newLineSumCb);
    }

    public static Counter newClass(CoverageMergeClass coverageMergeClass) {
        return coverageMergeClass.getCounters().stream()
                .filter(it -> it.getType() == CounterType.CLASS).findFirst()
                .orElseGet(() -> new Counter(CounterType.CLASS, 1, 0));
    }
}
